package com.ybfenpan.controller.admin.info;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.service.AdminService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.SysConst;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class UpdatePwdController extends BaseController {
    @Resource
    private AdminService adminService;
   
   

    @RequestMapping("admin/info/updatePwd.shtml")
    public String show(ModelMap model, HttpServletRequest request, HttpSession session) throws Exception{
        Map formMap = getParameterMap(request);
        
        Admin admin = ContextUtil.getAdmin();
        String myTwoPassword = MapUtil.getString(formMap, "yuanTwoPassword");
   	 	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
    	 
    	if(!admin.getTwoPassword().equals(pwd)){
    		setMessage(model,"原安全密码不正确。");
    		return "admin/info/updatePwd";
    		
    	}else{
    		//修改密码
    		String onePassword = MapUtil.getString(formMap, "onePassword");
       	 	String onePasswordKey = CryptUtil.getInstance().encrypt2String(onePassword,CryptUtil.keyStr);
       	 	String twoPassword = MapUtil.getString(formMap, "twoPassword");
    	 	String twoPasswordKey = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
    	 	
    		setMessage(model,"修改成功。");
    		Boolean result = true;
    		request.setAttribute("result", result);
    		//修改缓存
    		
    		
    		
    		Map mapAdmin = new HashMap();
    		mapAdmin.put("id",admin.getId());
    		mapAdmin.put("onePassword",onePasswordKey);
    		mapAdmin.put("twoPassword",twoPasswordKey);
    		adminService.update(mapAdmin);
    		
    		Admin admin2 = adminService.getByKey(admin.getId());
    		session.setAttribute(SysConst.MG, admin2);
    		
    		return "admin/info/updatePwd";
    	}
        
        
    }
    
    
     

     
    

}
